//
//  GPContactCell.m
//  Keping
//
//  Created by 23 on 2017/12/7.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPContactCell.h"
#import "GPContactModel.h"

@interface GPContactCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;
@property (weak, nonatomic) IBOutlet UIButton *statusButton;
@property (weak, nonatomic) IBOutlet UIView *flagView;

@end

@implementation GPContactCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    //添加长按手势
    UILongPressGestureRecognizer *lgGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(lgGesCell)];
    [self.contentView addGestureRecognizer:lgGes];
    
    self.flagView.layer.cornerRadius = 4.0;
    self.flagView.layer.masksToBounds = YES;
}

- (void)setType:(GPContactCellType)type
{
    _type = type;
    
    switch (type) {
        case GPContactCellTypeHistory:
        {
            self.statusButton.hidden = YES;
        }
            break;
        case GPContactCellTypeContact:
        {
            self.statusButton.hidden = NO;
        }
            break;
        default:
            break;
    }
}

- (void)setContact:(GPContactModel *)contact
{
    _contact = contact;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:[KGetImageURL stringByAppendingPathComponent:contact.profile]] placeholderImage:[UIImage imageNamed:@"head_portrait_blue_small"]];
    self.nameLabel.text = contact.nickName.length?contact.nickName:@" ";
    
    self.infoLabel.text = contact.subTitle.length?contact.subTitle:@" ";
    
    //0=add as friend 1=waiting for approval 2=friends 4=accept
    self.statusButton.enabled = NO;
    if (contact.status == 0) {
        [self.statusButton setTitle:@"add as friend" forState:UIControlStateNormal];
        self.statusButton.enabled = YES;
    }else if (contact.status == 1){
        [self.statusButton setTitle:@"waiting for approval" forState:UIControlStateNormal];
    }else if (contact.status == 2){
        [self.statusButton setTitle:@"friends" forState:UIControlStateNormal];
    }else if(contact.status == 4){
        [self.statusButton setTitle:@"accept" forState:UIControlStateNormal];
        self.statusButton.enabled = YES;
    }
    
    self.flagView.hidden = !contact.unRead;
    
}

/**点击添加好友按钮*/
- (IBAction)addFriends:(id)sender {
    
    if ((self.contact.status == 0) && self.callback) {
        self.callback(self.contact);
    }else if ((self.contact.status == 4) && self.acceptCallback){
        self.acceptCallback(self.contact);
    }
}

/**长按手势*/
- (void)lgGesCell
{
    if (self.longPressCallback) {
        self.longPressCallback(self.contact);
    }
}


@end










